typeof
와 keyof
를 활용해 타입 안전성을 갖춘 객체를 정의하고 활용할 수 있습니다.
실수 없이 안전하게 키와 값을 다루며, 디자인 시스템 같은 곳에 유용하게 사용할 수 있습니다.
키워드 | 설명 |
---|---|
typeof | 값을 기반으로 타입을 추출 |
keyof | 객체 타입의 키만 뽑아서 유니언 타입으로 만들기 |
const theme = {
colors: {
primary: '#3498db',
secondary: '#2ecc71',
danger: '#e74c3c'
}
}
typeof
로 객체의 타입을 추출type Theme = typeof theme;
// ✅ Theme 타입
// type Theme = {
// colors: {
// primary: string;
// secondary: string;
// danger: string;
// };
// }
✔️ Theme
타입은 theme
객체의 구조와 동일한 타입을 갖습니다.
keyof
로 유효한 키만 추출해서 사용type ColorKeys = keyof Theme['colors'];
// ✅ 해당 키 값들을 갖는 유니온 타입으로 지정
// type ColorKeys = "primary" | "secondary" | "danger"
function getColor(key: ColorKeys) {
return theme.colors[key];
}
getColor('primary'); // ✅
getColor('warning'); // ❌ 컴파일 에러! ('warning'은 없는 키)
theme
객체가 커져도 타입이 자동으로 따라감